package ch.ethz.inf.shop.model.simpleDatabase.operators.tests;

import org.junit.Assert;
import org.junit.Test;

import ch.ethz.inf.shop.model.simpleDatabase.Tuple;
import ch.ethz.inf.shop.model.simpleDatabase.operators.Scan;

/**
 * Makes some tests on Scan operator To use change the ROOT_DIRECTORY
 * 
 * @author rop
 */
public class TestScan {
	/**
	 * Reads the whole brand table
	 */
	@Test
	public void testScanBrand() {
		Scan scan = new Scan(AllTests.ROOT_DIRECTORY + "/brand.txt", "brand");
		Tuple current;

		Assert.assertTrue(scan.moveNext());
		current = scan.current();
		Assert.assertEquals(1, current.getInt(0));
		Assert.assertEquals("Nestle", current.getString(1));

		Assert.assertTrue(scan.moveNext());
		current = scan.current();
		Assert.assertEquals(2, current.getInt(0));
		Assert.assertEquals("Migros", current.getString(1));

		Assert.assertFalse(scan.moveNext());
	}

	/**
	 * read 6 lines and test for the correct 7th line
	 */
	@Test
	public void testScanRecipe() throws Exception {

		Scan scan = new Scan(AllTests.ROOT_DIRECTORY + "/recipe.txt", "recipe");

		Assert.assertTrue(scan.moveNext());
		Assert.assertTrue(scan.moveNext());
		Assert.assertTrue(scan.moveNext());
		Assert.assertTrue(scan.moveNext());
		Assert.assertTrue(scan.moveNext());
		Assert.assertTrue(scan.moveNext());
		Assert.assertTrue(scan.moveNext());
		Tuple current = scan.current();

		Assert.assertEquals(7, current.getInt(0));
		Assert.assertEquals("Chili con carne", current.getString(1));
		Assert.assertEquals(Tuple.HOUR_MINUTE_SECOND_FORMAT.parse("01:15:00"), current.getTime(2));
		Assert.assertEquals(
				"1. Fleisch kurz anbraten. 2. Gemuese hinzugeben. 3. Mit Bouillon abloeschen. 4. Wuerzen mit Paprika und auf kleiner Flamme koecheln lassen.",
				current.getString(3));

		Assert.assertFalse(scan.moveNext());
	}

	@Test
	public void testScanRecipeItem() {
		Scan scan = new Scan(AllTests.ROOT_DIRECTORY + "/recipe_item.txt", "recipe_item");
		int lineCnt = 0;
		while (scan.moveNext()) {
			lineCnt++;
		}
		Assert.assertEquals(18, lineCnt);
	}
}
